//
//  HJInformationBlockchainLayout.m
//  KaiChat
//
//  Created by lx on 2024/3/11.
//

#import "HJBlockchainInformationLayout.h"
#import "HJAttributedStringFactory.h"
 
@interface HJBlockchainInformationLayout()

@property (nonatomic, strong, readwrite) HJInformationModel *model;

@end


@implementation HJBlockchainInformationLayout

MJLogAllIvars

+ (HJBlockchainInformationLayout *)layoutWithModel:(HJInformationModel *)model {
    HJBlockchainInformationLayout *layout = [[HJBlockchainInformationLayout alloc] init];
    layout.model = model;
    [layout calculateLayout];
    return layout;
}

- (void)calculateLayout {
    
    NSLog(@"_model.images = %tu", _model.images.count);
    
    CGSize imageSize = CGSizeMake(120, 92);
    
    if (_model.images.count > 0){
        _imageRect = CGRectMake(KScreenWidth - leftRightMargin - imageSize.width, 16, imageSize.width, imageSize.height);
    } else {
        _imageRect = CGRectMake(KScreenWidth - leftRightMargin - imageSize.width, 16, 0, 0);
    }
    
    NSString *title = self.model.newsTitle;
    UIFont *titleFont = UIFontMediumMake(14);
    
    CGFloat titleTextMaxW = KScreenWidth - leftRightMargin * 2;
    
    if (_imageRect.size.height > 0) {
        titleTextMaxW = KScreenWidth - _imageRect.size.width - 10 - leftRightMargin * 2;
    }

    HJAttributedStringFactory *attributedStringFactory = [[HJAttributedStringFactory alloc] initWithString:title maxWidth:titleTextMaxW font:titleFont lineSpacing:3 alignment:NSTextAlignmentLeft];
    self.attributedString = attributedStringFactory.attributedString;
    CGSize titleTextSize = attributedStringFactory.size;
    if (attributedStringFactory.numberOfLines > 3) {
        titleTextSize.height = [attributedStringFactory numberLineHeight:3];
    }
 
    _titleRect = CGRectMake(leftRightMargin, _imageRect.origin.y, titleTextSize.width, titleTextSize.height);
 
    CGSize sourceLabelSize = [[NSString stringWithFormat:@"%@", self.model.source] getStringSizeWithWidth:HZoom(110) font:UIFontMake(12)];

    self.model.publishTime = [[NSDate dateWithTimeIntervalSince1970:self.model.publishTimestamp] stringWithFormat:@"dd MMM yyyy HH:mm"];
    
    CGSize timeLabelSize = [self.model.publishTime getStringSizeWithWidth:KScreenWidth - leftRightMargin * 2 font:UIFontMake(12)];
    sourceLabelSize.height = UIFontMake(12).lineHeight;
    
    if (_imageRect.size.height > 0) {
        _sourceRect = CGRectMake(_titleRect.origin.x,  CGRectGetMaxY(_imageRect) - sourceLabelSize.height, sourceLabelSize.width, sourceLabelSize.height);
    } else {
        _sourceRect = CGRectMake(_titleRect.origin.x,  CGRectGetMaxY(_titleRect) + 15, sourceLabelSize.width, sourceLabelSize.height);
    }
    
    _timeRect = CGRectMake(CGRectGetMaxX(_sourceRect) + 12, _sourceRect.origin.y, timeLabelSize.width, timeLabelSize.height);

    _operateRect = CGRectMake(leftRightMargin, CGRectGetMaxY(_timeRect) + 20, KScreenWidth - leftRightMargin * 2, 32);
    _height = CGRectGetMaxY(_operateRect) + 12;
}


@end
